package com.fenoco;


import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

public class FileUploadBean{
    
    private ArrayList<FileImage> files = new ArrayList<FileImage>();
    private int uploadsAvailable = 50;
    private boolean autoUpload = true;
    private boolean useFlash = false;
    private FileImage selectedFile;
    private int currentRow;
    
    
    public FileUploadBean() {
    }

    /**
     * Realiza el proceso par pintar las imagenes cargadas
     * @param stream
     * @param object
     * @throws IOException
     */
    public void paint(OutputStream stream, Object object) throws IOException {
        stream.write(getFiles().get((Integer)object).getData());
    }
    /**
     * Listener que permite recibir los evento de cargue para adicionar la imagen cargada
     * @param event
     * @throws Exception
     */
    public void listener(UploadEvent event) throws Exception{
        UploadItem item = event.getUploadItem();
        FileImage file = new FileImage();
        file.setLength(item.getData().length);
        file.setName(item.getFileName());
        file.setData(item.getData());
        files.add(file);
        uploadsAvailable--;
    }
    
	/**
	 * Metodo que elimina la imagen seleccionada
	 * @return null
	 */
	public String deleteImage(){
    	try{
    		if (currentRow >= 0){
    			files.remove(currentRow);
    		}
    	}catch (Exception e) {
    		showErr("Error al eliminar la imagen. ("+e.getMessage()+")");
		}
    	
    	return null;
    }
      
    /**
     * Elimina los datos de carga de las imagenes
     * @return
     */
    public String clearUploadData() {
        files.clear();
        setUploadsAvailable(5);
        return null;
    }
    
    public long getTimeStamp(){
        return System.currentTimeMillis();
    }
    
    /**
     * Muestra los mensajes de error
     * @param msg Mensaje
     */
    private void showErr(String msg) {
		FacesMessage fm = new FacesMessage();
		fm.setSummary(msg);
		fm.setSeverity(FacesMessage.SEVERITY_ERROR);
		FacesContext.getCurrentInstance().addMessage(null, fm);
	}
    
    public ArrayList<FileImage> getFiles() {
        return files;
    }

    public void setFiles(ArrayList<FileImage> files) { 
        this.files = files;
    }

    public int getUploadsAvailable() {
        return uploadsAvailable;
    }

    public void setUploadsAvailable(int uploadsAvailable) {
        this.uploadsAvailable = uploadsAvailable;
    }

    public boolean isAutoUpload() {
        return autoUpload;
    }

    public void setAutoUpload(boolean autoUpload) {
        this.autoUpload = autoUpload;
    }

    public boolean isUseFlash() {
        return useFlash;
    }

    public void setUseFlash(boolean useFlash) {
        this.useFlash = useFlash;
    }
    
    public FileImage getSelectedFile() {
		return selectedFile;
	}

	public void setSelectedFile(FileImage selectedFile) {
		this.selectedFile = selectedFile;
	}

	public int getSize() {
        if (getFiles().size()>0){
            return getFiles().size();
        }else 
        {
            return 0;
        }
    }

	public int getCurrentRow() {
		return currentRow;
	}

	public void setCurrentRow(int currentRow) {
		this.currentRow = currentRow;
	}
}